package com.ruoyi.project.relation.service.impl;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.relation.domain.OneWayBusinessRelationship;
import com.ruoyi.project.relation.domain.dto.OneWayBusinessRelationDO;
import com.ruoyi.project.relation.domain.qo.OneWayBusinessRelationshipQO;
import com.ruoyi.project.relation.mapper.OneWayBusinessRelationshipMapper;
import com.ruoyi.project.relation.service.IOneWayBusinessRelationshipService;
import com.ruoyi.project.wb.utils.TokenUserUtils;
import org.shoukaiseki.aqoeous.config.Validator;
import org.shoukaiseki.common.logger.Logger;
import org.shoukaiseki.common.logger.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BeanPropertyBindingResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 单向业务关联Service业务层处理
 *
 * @author wb
 * @date 2021-04-14
 */
@Service
public class OneWayBusinessRelationshipServiceImpl implements IOneWayBusinessRelationshipService {
    public static final Logger logger = LoggerFactory.getLogger(OneWayBusinessRelationshipServiceImpl.class);

    @Autowired
    private OneWayBusinessRelationshipMapper oneWayBusinessRelationshipMapper;

    @Resource
    private Validator validator;

    /**
     * 增加关联
     *
     * @param sourceBusinessRelationDO
     * @param targetBusinessRelationDO
     * @param sysUser
     * @param nowDate
     */
    @Transactional
    @Override
    public void addInterrelate(OneWayBusinessRelationDO sourceBusinessRelationDO, OneWayBusinessRelationDO targetBusinessRelationDO) {
        validator.validate(sourceBusinessRelationDO, new BeanPropertyBindingResult(targetBusinessRelationDO, "sourceBusinessRelationDO"), true);
        validator.validate(targetBusinessRelationDO, new BeanPropertyBindingResult(targetBusinessRelationDO, "targetBusinessRelationDO"), true);

        SysUser sysUser = TokenUserUtils.getUser();
        Date nowDate = DateUtils.getNowDate();

        OneWayBusinessRelationshipQO businessRelationSO = new OneWayBusinessRelationshipQO();
        businessRelationSO.setSourceOwnerId(sourceBusinessRelationDO.getOwnerId());
        businessRelationSO.setSourceOwnerName(sourceBusinessRelationDO.getOwnerName());
        businessRelationSO.setTargetOwnerId(targetBusinessRelationDO.getOwnerId());
        businessRelationSO.setTargetOwnerName(targetBusinessRelationDO.getOwnerName());
        businessRelationSO.setDelFlag(null);
        List<OneWayBusinessRelationship> businessRelationList = oneWayBusinessRelationshipMapper.exactQueryOneWayBusinessRelationshipList(businessRelationSO);
        OneWayBusinessRelationship businessRelationTmp = businessRelationList.stream().findFirst().orElse(null);
        if (businessRelationTmp != null) {
            //删除标志
            if (businessRelationTmp.getDelFlag().intValue() == 1) {
                businessRelationTmp.setDelFlag(0);
                BaseEntity.setUpdate(businessRelationTmp, sysUser, nowDate);
                oneWayBusinessRelationshipMapper.updateOneWayBusinessRelationship(businessRelationTmp);
            }
            return;
        }
        businessRelationTmp = new OneWayBusinessRelationship();
        BeanUtils.copyProperties(businessRelationSO, businessRelationTmp);
        BaseEntity.setCreate(businessRelationTmp, sysUser, nowDate);
        oneWayBusinessRelationshipMapper.insertOneWayBusinessRelationship(businessRelationTmp);
    }


    /**
     * 删除关联,所属业务数据被删除,同时删除关系
     *
     * @param oneWayBusinessRelationshipQO
     */
    @Transactional
    @Override
    public void deleteOneWayBusinessRelationshipByOwner(OneWayBusinessRelationDO businessRelationDO) {
        validator.validate(businessRelationDO, new BeanPropertyBindingResult(businessRelationDO, "sourceBusinessRelationDO"), true);
        OneWayBusinessRelationshipQO oneWayBusinessRelationshipQOTmp = new OneWayBusinessRelationshipQO();
        oneWayBusinessRelationshipQOTmp.setSourceOwnerId(businessRelationDO.getOwnerId());
        oneWayBusinessRelationshipQOTmp.setSourceOwnerName(businessRelationDO.getOwnerName());
        oneWayBusinessRelationshipMapper.deleteOneWayBusinessRelationshipByOneWayBusinessRelationshipQO(oneWayBusinessRelationshipQOTmp);

        oneWayBusinessRelationshipQOTmp = new OneWayBusinessRelationshipQO();
        oneWayBusinessRelationshipQOTmp.setTargetOwnerId(businessRelationDO.getOwnerId());
        oneWayBusinessRelationshipQOTmp.setTargetOwnerName(businessRelationDO.getOwnerName());
        oneWayBusinessRelationshipMapper.deleteOneWayBusinessRelationshipByOneWayBusinessRelationshipQO(oneWayBusinessRelationshipQOTmp);
    }


    /**
     * 删除关联
     *
     * @param oneWayBusinessRelationshipQO
     */
    @Transactional
    @Override
    public void deleteOneWayBusinessRelationship(OneWayBusinessRelationDO sourceBusinessRelationDO, OneWayBusinessRelationDO targetBusinessRelationDO) {
        validator.validate(sourceBusinessRelationDO, new BeanPropertyBindingResult(sourceBusinessRelationDO, "sourceBusinessRelationDO"), true);
        validator.validate(targetBusinessRelationDO, new BeanPropertyBindingResult(targetBusinessRelationDO, "targetBusinessRelationDO"), true);
        OneWayBusinessRelationshipQO oneWayBusinessRelationshipQOTmp = new OneWayBusinessRelationshipQO();
        oneWayBusinessRelationshipQOTmp.setSourceOwnerId(sourceBusinessRelationDO.getOwnerId());
        oneWayBusinessRelationshipQOTmp.setSourceOwnerName(sourceBusinessRelationDO.getOwnerName());
        oneWayBusinessRelationshipQOTmp.setTargetOwnerId(targetBusinessRelationDO.getOwnerId());
        oneWayBusinessRelationshipQOTmp.setTargetOwnerName(targetBusinessRelationDO.getOwnerName());

        oneWayBusinessRelationshipMapper.deleteOneWayBusinessRelationshipByOneWayBusinessRelationshipQO(oneWayBusinessRelationshipQOTmp);
    }

    /**
     * 新增单向业务关联
     *
     * @param oneWayBusinessRelationship 单向业务关联
     * @return 主键
     */
    @Transactional
    @Override
    public Long insertOneWayBusinessRelationship(OneWayBusinessRelationship oneWayBusinessRelationship)
    {

        SysUser user = TokenUserUtils.getUser();
        BaseEntity.setCreate(oneWayBusinessRelationship,user,DateUtils.getNowDate());
        oneWayBusinessRelationshipMapper.insertOneWayBusinessRelationship(oneWayBusinessRelationship);
        return oneWayBusinessRelationship.getOneWayBusinessRelationshipId();
    }


    /**
     * 批量删除单向业务关联
     *
     * @param oneWayBusinessRelationshipIds 需要删除的单向业务关联ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOneWayBusinessRelationshipByIds(Long... oneWayBusinessRelationshipIds)
    {
        return oneWayBusinessRelationshipMapper.deleteOneWayBusinessRelationshipByIds(oneWayBusinessRelationshipIds);
    }


}
